StreamWriter constructors.

The following example demonstrates the StreamWriter constructors.

C# .NET

public static string DoTest()
{
         return CreateTextFile(@"c:\Temp\StreamWriter_Construct.txt",
                                                               @"Some text to test StreamWriter constructors");
}

public static string CreateTextFile(string fileName, string textToAdd)
{
         if(File.Exists(fileName))
                  File.Delete(fileName);
         string logFile = DateTime.Now.ToShortDateString().Replace(@"/",@"-").Replace(@"\",@"-") + ".log";

         try
         {
                  FileStream fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write, FileShare.None);
         
                  StreamWriter swFromFile = new StreamWriter(logFile);
                  swFromFile.Write(textToAdd);
                  swFromFile.Flush();
                  swFromFile.Close();

                  StreamWriter swFromFileStream = new StreamWriter(fs);
                  swFromFileStream.Write(textToAdd);
                  swFromFileStream.Flush();
                  swFromFileStream.Close();

                  StreamWriter swFromFileTrue = new StreamWriter(fileName,true);
                  swFromFileTrue.Write(textToAdd);
                  swFromFileTrue.Flush();
                  swFromFileTrue.Close();
         
                  StreamWriter swFromFileTrueUTF8Buffer = new StreamWriter(fileName, true, System.Text.Encoding.UTF8,512);
                  swFromFileTrueUTF8Buffer.Write(textToAdd);
                  swFromFileTrueUTF8Buffer.Flush();
                  swFromFileTrueUTF8Buffer.Close();

                  StreamWriter swFromFileTrueUTF8 = new StreamWriter(fileName, true, System.Text.Encoding.UTF8);
                  swFromFileTrueUTF8.Write(textToAdd);
                  swFromFileTrueUTF8.Flush();
                  swFromFileTrueUTF8.Close();

         }
         catch(Exception e)
         {
                  return ("Text file could not be created, cause: " + e.Message);
         }
         return "Text file was successfully created";
}

 

Blaze++ .NET

static String DoTest()
{
         return CreateTextFile("c:\\Temp\\StreamWriter_Construct.txt",
                                                               "Some text to test StreamWriter constructors");
}

static String CreateTextFile(String fileName, String textToAdd)
{
         if(File::Exists(fileName))
                  File::Delete(fileName);
         String logFile = DateTime::Now.ToShortDateString().Replace("/", "-").Replace( "\\", "-") + ".log";

         try
         {
                  FileStream fs(fileName, FileMode::CreateNew, FileAccess::Write, FileShare::None);
         
                  StreamWriter swFromFile(logFile);
                  swFromFile.Write(textToAdd);
                  swFromFile.Flush();
                  swFromFile.Close();

                  StreamWriter swFromFileStream(fs);
                  swFromFileStream.Write(textToAdd);
                  swFromFileStream.Flush();
                  swFromFileStream.Close();
                  fs.Close();

                  StreamWriter swFromFileTrue(fileName, true);
                  swFromFileTrue.Write(textToAdd);
                  swFromFileTrue.Flush();
                  swFromFileTrue.Close();
         
                  StreamWriter swFromFileTrueUTF8Buffer(fileName, true, System::Text::Encoding::UTF8, 512);
                  swFromFileTrueUTF8Buffer.Write(textToAdd);
                  swFromFileTrueUTF8Buffer.Flush();
                  swFromFileTrueUTF8Buffer.Close();

                  StreamWriter swFromFileTrueUTF8(fileName, true, System::Text::Encoding::UTF8);
                  swFromFileTrueUTF8.Write(textToAdd);
                  swFromFileTrueUTF8.Flush();
                  swFromFileTrueUTF8.Close();

         }
         catch(Exception e)
         {
                  return ("Text file could not be created, cause: " + e.Message);
         }
         return "Text file was successfully created";
}